﻿Imports DevExpress.XtraTreeList.Nodes
Imports DevExpress.XtraTreeList
Imports DevExpress.Utils.Design
Imports System.Linq
Imports System.Reflection


Public Class ucTesourariaTree
    Public CountNodes As Integer
    Public BarGroup As String = "GroupTesourariaClientes"

    Public Sub New()
        InitializeComponent()
        InitData()
    End Sub
   
    Private Sub InitData()

        Dim db As New MenuLinqDataContext

        Dim grupo = (From c In db.tblUserGrpMap1s _
                   Where c.UserId = GlobalVariables.UserName _
                   Select c.GroupCode).SingleOrDefault

        Dim Nivel1 = (From c In db.tblMenuLists Join d In db.tblGroupDesigns On d.MenuName Equals c.MenuName _
                      Where c.Nivel = "1" AndAlso c.BarGroup = BarGroup AndAlso d.GroupCode = grupo _
                      Select c)
        For Each nome In Nivel1
            Dim tl As TreeListNode = treeList1.AppendNode(New Object() {nome.MenuDesc}, nome.MainMenuName)
        Next

        Dim Nivel2 = (From c In db.tblMenuLists Join d In db.tblGroupDesigns On d.MenuName Equals c.MenuName _
                      Where c.Nivel = "2" AndAlso c.BarGroup = BarGroup AndAlso d.GroupCode = grupo _
                      Select c)
        For Each nome In Nivel2
            Dim tl As TreeListNode = treeList1.AppendNode(New Object() {nome.MenuDesc}, treeList1.FindNodeByFieldValue("Name", nome.MainMenuName))
        Next

        Dim Nivel3 = (From c In db.tblMenuLists Join d In db.tblGroupDesigns On d.MenuName Equals c.MenuName _
                      Where c.Nivel = "3" AndAlso c.BarGroup = BarGroup AndAlso d.GroupCode = grupo _
                      Select c)
        For Each nome In Nivel3
            Dim tl As TreeListNode = treeList1.AppendNode(New Object() {nome.MenuDesc}, treeList1.FindNodeByFieldValue("Name", nome.MainMenuName))
        Next

        CountNodes = Nivel1.Count.ToString
    End Sub

    Private Sub treeList1_FocusedNodeChanged(sender As Object, e As FocusedNodeChangedEventArgs) Handles treeList1.FocusedNodeChanged
        Dim db As New MenuLinqDataContext

        Dim Child As String = e.Node.HasChildren.ToString
        If CountNodes = treeList1.Nodes.Count.ToString() Then
            Dim a As String = e.Node.GetDisplayText(0)
            If Child = False Then
                Dim LinkForm = (From c In db.tblMenuLists _
                               Where c.MenuDesc = a _
                               Select c.Form).SingleOrDefault
                If LinkForm <> "" Then
                    Dim FullTypeName As String = Application.ProductName & "." & LinkForm.ToString
                    Dim FormInstanceType As Type = Type.GetType(FullTypeName, True, True)
                    Dim objForm As Form = CType(Activator.CreateInstance(FormInstanceType), Form)
                    objForm.MdiParent = FMain
                    objForm.Show()
                End If
            End If
        End If

    End Sub

End Class
